package com.dengqi.mymodule.activity.effect;

import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.dengqi.mymodule.R;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 1.通过StateListDrawable设置点击效果
 * 2.通过setColorFilter设置点击效果
 * https://www.jianshu.com/p/ebda27f061e3
 * https://blog.csdn.net/weixin_41101173/article/details/80146504
 * https://blog.csdn.net/lizzywu/article/details/17505423/
 * https://www.jianshu.com/p/b4031c9a16d8
 */
public class EffectActivity extends RxAppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    @BindView(R.id.iv_pic)
    ImageView iv_pic;
    @BindView(R.id.tv_color)
    TextView tv_color;
    @BindView(R.id.iv_color)
    ImageView iv_color;
    @BindView(R.id.sb_r)
    SeekBar sb_r;
    @BindView(R.id.sb_g)
    SeekBar sb_g;
    @BindView(R.id.sb_b)
    SeekBar sb_b;
    @BindView(R.id.sb_a)
    SeekBar sb_a;
    ColorMatrix matrix;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_effect);
        ButterKnife.bind(this);

        matrix = new ColorMatrix();
        sb_r.setOnSeekBarChangeListener(this);
        sb_g.setOnSeekBarChangeListener(this);
        sb_b.setOnSeekBarChangeListener(this);
        sb_a.setOnSeekBarChangeListener(this);
//        ColorMatrix cm = new ColorMatrix();
//        cm.set(new float[] {
//                1, 0, 0, 0, 0,
//                0, 1, 0, 0, 0,
//                0, 0, 1, 0, 0,
//                0, 0, 0, 1, 0 });
//        Drawable drawable = view.getResources().getDrawable(R.drawable.shape_view_click);
//        GradientDrawable drawableRed = new GradientDrawable();
//        drawableRed.setShape(GradientDrawable.RECTANGLE);
//        drawableRed.setColor(Color.parseColor("#8000ff00")); //.setColorFilter(new ColorMatrixColorFilter(cm)); //(Color.parseColor("#8000ff00"));
//        StateListDrawable stateListDrawable = new StateListDrawable();
//        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, drawableRed);
//        stateListDrawable.addState(new int[]{-android.R.attr.state_pressed}, view.getBackground());
//        view.setBackground(stateListDrawable);
    }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        //设置偏移量
        matrix.setScale(caculate(sb_r.getProgress()), caculate(sb_g.getProgress()),
                caculate(sb_b.getProgress()), caculate(sb_a.getProgress()));
        //偏向颜色的值
        tv_color.setText("#" + Integer.toHexString(sb_a.getProgress())
                + Integer.toHexString(sb_r.getProgress())
                + Integer.toHexString(sb_g.getProgress())
                + Integer.toHexString(sb_b.getProgress()));

        //用来显示偏向的颜色
        iv_color.setBackgroundColor(Color.argb(sb_a.getProgress(),
                sb_r.getProgress(),
                sb_g.getProgress(),
                sb_b.getProgress()));

        //为组件设置新的过滤器
        iv_pic.setColorFilter(new ColorMatrixColorFilter(matrix));
    }

    float caculate(int progress) {
        return (float) progress;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}
